package alpha.com;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Set;

/**
 * Created by Justin on 2015/8/11.
 */
public class JDrawPanel extends JPanel {
    private Image m_img;
    private int m_width;
    private int m_height;
    private Graphics m_graphics;

    /** Constructs a new JDrawPanel. */
    public JDrawPanel() {
        m_width = 400;
        m_height = 400;
        m_img = new BufferedImage(m_width, m_height, BufferedImage.TYPE_INT_RGB);
        m_graphics = m_img.getGraphics();

    }

    public void draw() {
        m_graphics.setColor(Color.RED);
        m_graphics.fillRect(0, 0, 100, 100);
        m_graphics.fillRect(100, 100, 100, 100);
        m_graphics.fillRect(200, 200, 100, 100);
        m_graphics.fillRect(300, 300, 100, 100);
    }

    public void drawOnePoint(int[] input, LabelName c) {
        switch (c) {
            case RED:
                m_graphics.setColor(Color.RED);
                m_graphics.fillRect(input[0], input[1], 2, 2);
                break;
            case GREEN:
                m_graphics.setColor(Color.GREEN);
                m_graphics.fillRect(input[0], input[1], 2, 2);
                break;
            case BLUE:
                m_graphics.setColor(Color.BLUE);
                m_graphics.fillRect(input[0], input[1], 2, 2);
                break;
        }
        this.updateUI();
    }


    public void drawPoints(Set<int[]> input, LabelName c) {
        switch (c) {
            case RED:
                m_graphics.setColor(Color.RED);
                break;
            case GREEN:
                m_graphics.setColor(Color.GREEN);
                break;
            case BLUE:
                m_graphics.setColor(Color.BLUE);

                break;
        }
        for (int[] point : input) {
            m_graphics.fillRect(point[0], point[1], 2, 2);
        }

    }

    public void drawResult(int[][] result) {
        for (int x = 0; x < 400; x++) {
            for (int y = 0; y < 400; y++) {
                int color = result[x][y];
                switch (color) {
                    case 0:
                        m_graphics.setColor(Color.PINK);
                        break;
                    case 1:
                        m_graphics.setColor(Color.yellow);
                        break;
                    case 2:
                        m_graphics.setColor(Color.cyan);
                        break;
                }
                m_graphics.fillRect(x, y, 1, 1);
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(m_img, 0, 0, this.getWidth(), this.getHeight(), this);
    }

    public void clear() {
        m_graphics.setColor(Color.black);
        m_graphics.fillRect(0, 0, m_width, m_height);
        this.updateUI();
    }

}
